/**
 * 功能管理
 * @constructor
 */
function Button() {
}

Button.init = function () {
    let vm = new Vue({
        el: "#customModule",
        data: {
            buttonDescription: "",
            activity: "",
            buttonID: "",
            enDesc: "",
            menuName: "",
            menuID: "",
            menuNameCondition: "",
            funNameCondition: "",
            activityCondition: "-1",
            clickState: "",
            roleList: "",
            roleChecked: "1"
        },
        created() {
            document.getElementById("activityCondition")[0].selected = true;
            self = this;
            window.changeState = self.changeState;
            window.edit = self.edit;
            window.del = self.del;
        },
        mounted() {
            this.initBootStrapSource();
            this.initRole();
        },
        computed: {},
        watch: {},
        methods: {
            /**
             * 获取当前角色下，可供选择的角色
             */
            initRole() {
                let url = "/RoleManage/initRole?roleID=" + $.cookie('RoleIds');
                axios.get(url).then(data => {
                    if (data.data.code === "200") {
                        this.roleList = [];
                        for (let i = 0; i < data.data.data.length; i++) {
                            let result = data.data.data[i];
                            let param = {
                                id: result.roleID,
                                name: result.roleName
                            };
                            this.roleList.push(param);
                        }
                    }
                }).catch(e => {

                })
            },
            initBootStrapSource() {
                $('#tableList').bootstrapTable({
                    url: '/buttonManage/tableSearch',
                    method: 'post',
                    toolbar: "#toolbar",
                    sidePagination: 'server',
                    dataField: "data",
                    striped: true,
                    pagination: true,
                    pageSize: "10",
                    pageList: [10, 25, 50, 100],
                    sortable: true,
                    sortName: 'menuID',
                    sortOrder: "asc",
                    search: false,
                    showRefresh: true,
                    showColumns: true,
                    showExport: true,
                    showToggle: true,
                    queryParams: function queryParams(params) {
                        return {
                            pageSize: params.limit,                         //页面大小
                            pageIndex: (params.offset / params.limit) + 1,   //页码
                            sort: params.sortName,      //排序列名
                            sortOrder: params.sortOrder, //排位命令（desc，asc）
                            buttonDescription: $('#funNameCondition').val(),
                            menuName: $('#menuNameCondition').val(),
                            activity: $('#activityCondition').val(),
                            roleID: $.cookie('RoleIds')
                        };
                    },
                    onLoadSuccess: function (data) {
                        Help.alertSuccMessage("数据加载完毕");
                    },
                    onLoadError: function (status) {
                    },
                    columns: [
                        {
                            title: '序号',//标题  可不加
                            align: "center",
                            width: 40,
                            formatter: function (value, row, index) {
                                var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                                var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                                return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                            }
                        },
                        {field: 'id', title: '功能编号', align: 'center', sortable: true},
                        {field: 'buttonDescription', title: '功能描述', align: 'center', sortable: true},
                        {field: 'enDesc', title: '逻辑key', align: 'center', sortable: true},
                        {field: 'menuName', title: '菜单名称', align: 'center', sortable: true},
                        {field: 'menuID', title: '菜单编号', align: 'center', sortable: true},
                        {field: 'roleName', title: '角色名称', align: 'center', sortable: true},
                        {field: 'roleID', title: '角色编号', align: 'center', visible: false},
                        {
                            field: 'activity', title: '是否激活', align: 'center', sortable: true,
                            formatter: function (value, row, index) {
                                let optionHtml = "";
                                if (row.activity === 0) {
                                    optionHtml = "<span class=\"label label-danger custom\" style=\"cursor:pointer\" onclick='changeState(" + JSON.stringify(row) + ")'  title=\"未激活\" >未激活</span>";
                                } else if (row.activity === 1) {
                                    optionHtml = "<span class=\"label label-info\" style=\"cursor:pointer\" onclick='changeState(" + JSON.stringify(row) + ")' title=\"已激活\">已激活</span>";
                                }
                                return optionHtml;

                            }
                        },
                        {
                            title: '操作', align: 'center',
                            formatter: function (value, row, index) {
                                let optionHtml;
                                optionHtml = "<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='edit(" + JSON.stringify(row) + ")'  title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                                optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='del(" + JSON.stringify(row) + ")' title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>";
                                return optionHtml;
                            }
                        }
                    ]
                });
            },
            /**
             * 查询
             */
            search() {
                vm.refresh();
            },
            /**
             * 重置
             */
            reset() {
                vm.funNameCondition = "";
                vm.menuNameCondition = "";
                vm.activityCondition = "";
            },
            /**
             * 点击改变状态
             * @param data
             */
            changeState(data) {
                let url = "/buttonManage/changeState";
                let pm = {
                    activity: data.activity === 1 ? 0 : 1,
                    id: data.buttonID
                };
                axios.post(url, pm).then(data => {
                    if (data.data.code === "200") {
                        Help.alertInfoMessage("", "提交成功");
                        this.refresh();
                    }
                }).catch(e => {
                    console.log(e);
                })
            },
            /**
             * 编辑
             * @param data
             */
            edit(data) {
                vm.roleChecked = data.roleID;
                vm.buttonDescription = data.buttonDescription;
                vm.activity = data.activity;
                vm.buttonID = data.buttonID;
                vm.enDesc = data.enDesc;
                vm.menuName = data.menuName;
                vm.menuID = data.menuID;
                vm.clickState = "edit";
                $('#myModalAdd').modal('show');
            },
            /**
             * 删除
             * @param data
             */
            del(data) {
                swal({
                    title: "确认是否删除",
                    text: "是否确认要删除该条记录？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "删除",
                    cancelButtonText: "取消",
                    closeOnConfirm: false
                }, function () {
                    let url = "/buttonManage/del";
                    let pm = {
                        buttonID: data.buttonID,
                        roleID: data.roleID
                    };
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            swal("删除成功!", "请继续操作", "success");
                            vm.refresh();
                        } else {
                            swal("删除失败!", "请重试", "error");
                        }
                    }).catch(e => {
                        console.log(e);
                    })
                });
            },
            /**
             * 新增
             */
            addRecord() {
                vm.buttonDescription = "";
                vm.activity = "1";
                vm.buttonID = "";
                vm.enDesc = "";
                vm.menuName = "";
                vm.menuID = "";
                vm.clickState = "add";
                $('#myModalAdd').modal('show');
            },
            /**
             * 保存
             */
            saveRecord() {
                if (vm.clickState === "edit") {
                    let url = "/buttonManage/edit";
                    let pm = {
                        buttonDescription: vm.buttonDescription,
                        activity: vm.activity,
                        buttonID: vm.buttonID,
                        enDesc: vm.enDesc,
                        menuName: vm.menuName,
                        menuID: vm.menuID,
                        roleID: vm.roleChecked,
                        id: vm.id
                    };
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            Help.alertInfoMessage("", "提交成功");
                            this.refresh();
                        }
                    }).catch(e => {
                        console.log(e);
                    }).finally(() => {
                        $('#myModalAdd').modal('hide');
                        this.refresh();
                    })
                } else if (vm.clickState === "add") {
                    let url = "/buttonManage/add";
                    let pm = {
                        buttonDescription: vm.buttonDescription,
                        activity: vm.activity,
                        buttonID: vm.buttonID,
                        enDesc: vm.enDesc,
                        menuName: vm.menuName,
                        menuID: vm.menuID,
                        roleID: vm.roleChecked
                    };
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            Help.alertInfoMessage("", "提交成功");
                            this.refresh();
                        }
                    }).catch(e => {
                        console.log(e);
                    }).finally(() => {
                        $('#myModalAdd').modal('hide');
                        this.refresh();
                    })
                }
            },
            refresh() {
                $('#tableList').bootstrapTable(('refresh'));
            }
        }
    });
};

